package com.yonyou.pmclouds.workflow.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.workflow.entity.ProcessDefinitionVO;
import com.yonyou.pmclouds.workflow.util.WorkFlowConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import yonyou.bpm.rest.exception.RestException;
import yonyou.bpm.rest.request.AssignCheckParam;
import yonyou.bpm.rest.request.runtime.ProcessInstanceParam;

import java.util.Map;

@Slf4j
@Component
public class ProcessRuntimeClientService {

    @Autowired
    private ProcessClientService processService;

    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    /**
     * 流程启动时检查指派
     */
    public Map<String, Object> assignCheck(ProcessDefinitionVO processDefinitionVO, SuperVO businessVO) throws BusinessException {
        // 企业信息添加
        enterpriseInfoQuery.tranEntInfoField(new SuperVO[] {businessVO},new String[]{CommonFieldConst.PK_ENTERPRISE},
                new String[]{CommonFieldConst.ENTERPRISE_NAME});

        AssignCheckParam assignCheckParam = new AssignCheckParam();
        assignCheckParam.setTaskVariables(processService.getVariableList(businessVO));
        assignCheckParam.setProcessDefinitionKey(processDefinitionVO.getProcessKey());
        JsonNode jsonNode;
        try {
            long startTime = System.currentTimeMillis();
            Object resultObj = processService.getDefaultRuntimeService(RuntimeEnvironment.getLoginUserId(),
                    RuntimeEnvironment.getTenantId()).assignCheck(assignCheckParam);
            log.debug("调用云审指派检查方法耗时：" + (System.currentTimeMillis() - startTime));
            if (resultObj == null)
                throw new BusinessException(WorkFlowConst.YS_NULL_RESP_MSG);
            jsonNode = (JsonNode) resultObj;
            return processService.getAssignInfo(jsonNode, processDefinitionVO, businessVO);

        } catch (RestException e) {
            throw new BusinessException("指派检查失败 : " + e.getMessage());
        }
    }

    /**
     * 根据业务主键获取单据已启用的流程
     *
     * @param pkBill 单据主键
     */
    public JsonNode getProcessInstances(String pkBill) throws BusinessException {
        ProcessInstanceParam param = new ProcessInstanceParam();
        param.setBusinessKey(pkBill);
        JsonNode jsonNode = null;
        try {
            Object obj = processService.getDefaultRuntimeService(RuntimeEnvironment.getLoginUserId(),
                    RuntimeEnvironment.getTenantId()).getProcessInstances(param);
            if (obj == null)
                throw new BusinessException(WorkFlowConst.YS_NULL_RESP_MSG);
            jsonNode = (JsonNode) obj;
        } catch (RestException e) {
            log.error(e.getMessage(), e);
        }
        return jsonNode;
    }

   public ProcessDefinitionVO getDefinitionVOByType(String billType, SuperVO businessVO) throws BusinessException{
        return processService.getDefinitionByType(billType, businessVO);
   }
}
